package com.example.widgetcase.slice;

import com.example.widget_lib.widget.animator.BmpOverturnView;
import com.example.widgetcase.ResourceTable;
import com.example.widgetcase.utils.LogUtil;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.AnimatorGroup;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.Button;

public class BmpOverturnAbilitySlice extends BaseSlice implements AnimatorValue.ValueUpdateListener {
    private String TAG = this.getClass().getSimpleName();
    Button bovBtn;
    BmpOverturnView bov;
    private AnimatorValue bottomFlipAnimator;
    private AnimatorValue rotationFlipAnimator;
    private AnimatorValue topFlipAnimator;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        bovBtn = (Button) findComponentById(ResourceTable.Id_bovBtn);
        bov = (BmpOverturnView) findComponentById(ResourceTable.Id_bov);
        bovBtn.setClickedListener(component -> {
            bov.propReset();
            startAnim(false);
        });
        startAnim(true);
    }

    @Override
    public int getChildUIContent() {
        return ResourceTable.Layout_ability_bmp_overturn_slice;
    }

    private void startAnim(boolean isFirst) {
        //z侧暂无自定义属性值的动画？？？？？
//        AnimatorProperty bottomFlipAnimator = bov.createAnimatorProperty();

//        bov.setBottomFlip(30f);
//        bottomFlipAnimator.setDuration(10000);


//        bov.setTopFlip(-30f);
//        AnimatorProperty topFlipAnimator = bov.createAnimatorProperty();
//        topFlipAnimator.setDelay(200);
//        topFlipAnimator.setDuration(10000);

//        bov.setFlipRotation(270f);
//        AnimatorProperty rotationFlipAnimator = bov.createAnimatorProperty();
//        rotationFlipAnimator.setDelay(200);
//        rotationFlipAnimator.setDuration(10000);

//        AnimatorGroup animatorGroup = new AnimatorGroup();
//        animatorGroup.runSerially(bottomFlipAnimator,rotationFlipAnimator,topFlipAnimator);
//        if (isFirst) {
//            animatorGroup.setDelay(1000);
//        }
//        animatorGroup.start();


        bottomFlipAnimator = new AnimatorValue();
        bottomFlipAnimator.setDuration(1000);
        bottomFlipAnimator.setValueUpdateListener(this::onUpdate);

        rotationFlipAnimator = new AnimatorValue();
        rotationFlipAnimator.setDuration(1000);
        rotationFlipAnimator.setDelay(200);
        rotationFlipAnimator.setValueUpdateListener(this::onUpdate);

        topFlipAnimator = new AnimatorValue();
        topFlipAnimator.setDuration(1000);
        topFlipAnimator.setDelay(200);
        topFlipAnimator.setValueUpdateListener(this::onUpdate);

        AnimatorGroup animatorGroup = new AnimatorGroup();
        animatorGroup.runSerially(bottomFlipAnimator, rotationFlipAnimator, topFlipAnimator);
        if (isFirst) {
            animatorGroup.setDelay(1000);
        }
        animatorGroup.start();
    }


    @Override
    public void onUpdate(AnimatorValue animatorValue, float percent) {
        if (bov != null) {
            if (animatorValue == bottomFlipAnimator) {
                bov.setBottomFlip(30f * percent);
                LogUtil.loge(TAG, "bottomFlipAnimator ->"+ 30f * percent);
            } else if (animatorValue == topFlipAnimator) {
                bov.setTopFlip(-30f * percent);
                LogUtil.loge(TAG, "topFlipAnimator ->"+ -30f * percent);
            } else if (animatorValue == rotationFlipAnimator) {
                bov.setFlipRotation(270f * percent);
                LogUtil.loge(TAG, "rotationFlipAnimator ->"+ 270f * percent);
            }
        }

    }
}
